<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 教师首页接口
 */
class Teacher extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 首页
     */
    public function index()
    {
        $uid = $this->auth->id;
        $banner = Db::name('banner')->where('status',1)->select();
        foreach ($banner as &$vs) {
            $vs['img_full'] = cdnurl($vs['img'],true);
        }
        unset($vs);
        $parr = Db::name('teacher_collect')->where(['uid'=>$uid,'status'=>1])->column('pid');
        $where = [];
        $where['productId'] = ['in',$parr]; 
        $query1 = Db::name('hear_say_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query1 as &$vv) {
            $vv['module'] = 'hearsay';
        }
        unset($vv);
        $query2 = Db::name('listening_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query2 as &$vv) {
            $vv['module'] = 'listening';
        }
        unset($vv);
        $query3 = Db::name('repeat_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query3 as &$vv) {
            $vv['module'] = 'repeat';
        }
        unset($vv);
        $query4 = Db::name('writing_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query4 as &$vv) {
            $vv['module'] = 'writing';
        }
        unset($vv);
        $query5 = Db::name('soundmark_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query5 as &$vv) {
            $vv['module'] = 'soundmark';
        }
        unset($vv);
        $query6 = Db::name('model_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query6 as &$vv) {
            $vv['module'] = 'modelsp';
        }
        unset($vv);
        $query7 = Db::name('new_concept_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query7 as &$vv) {
            $vv['module'] = 'newconcept';
        }
        unset($vv);
        $query8 = Db::name('black_plum_products')->field('productId,productName,cover,periodName,gradeName')->where($where)->select();
        foreach ($query8 as &$vv) {
            $vv['module'] = 'blackplum';
        }
        unset($vv);
        $collect = array_merge($query1,$query2,$query3,$query4,$query5,$query6,$query7,$query8);
        foreach ($collect as &$vs) {
            $vs['cover_full'] = cdnurl($vs['cover'],true);
        }
        unset($vs);
        $data = [
            'banner'=>$banner,
            'collect'=>$collect,
            'path' => 'pages/phoneticPractice/list',
        ];
        //获取教师选择的教材和单元信息
        $where1 = [
            'user_id' => $this->auth->id,
        ];
        $data['unit'] = Db::name('textbook_synchronization_catalogues_log')
            ->alias('tscl')
            ->join('textbook_synchronization_catalogues tsc','tscl.unitid=tsc.unitid','LEFT')
            ->join('textbook_synchronization_products tsp','tscl.versionId=tsp.versionId','LEFT')
            ->where($where1)
            ->field('tscl.*,tsc.name,tsp.book_name')
            ->find();
        $this->success('请求成功', $data);
    }

    /**
     * 收藏书籍
     */
    public function collect()
    {
        $id = input('id/d');
        $status = input('status');
        $uid = $this->auth->id;
        if (!$id||$status=='') {
            $this->error(__('Invalid parameters'));
        }
        $info = Db::name('teacher_collect')->where(['uid'=>$uid,'pid'=>$id])->find();
        if ($info) {
            Db::name('teacher_collect')->where(['uid'=>$uid,'pid'=>$id])->update(['status'=>$status]);
        }else{
            Db::name('teacher_collect')->insert(['uid'=>$uid,'pid'=>$id,'status'=>$status,'createtime'=>date('Y-m-d H:i:s')]);
        }
        if ($status==1) {
            $this->success('收藏成功','');
        }else{
            $this->success('取消收藏成功','');
        }
    }

}
